package com.hengpeng.itfinoss.service.user.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.persist.QueryResult;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.model.Order.OrderType;
import com.hengpeng.itfinbase.persist.UserInfo;
import com.hengpeng.itfinbase.persist.UserInfo.UserInfoType;
import com.hengpeng.itfinoss.service.ServiceFactory;
import com.hengpeng.itfinoss.service.user.UserInfoService;

@Service
@Lazy
public class UserInfoServiceImpl implements UserInfoService
{
	@Autowired
	private ServiceFactory serviceFactory;
	@Override
	public ServiceResult<List<UserInfo>> selectUserPage(String userName, String mobile, UserInfoType type, String cardNumber,
			String trustAccountId,int indexPage, int pageSize, OrderType order, String orderField)
	{
		DynamicQuery query = new DynamicQuery(UserInfo.class);
		if (userName != null && userName.trim().length() > 0)
			query.eq("userName", userName);
		if (mobile != null && mobile.trim().length() > 0)
			query.eq("mobile", mobile);
		if (cardNumber != null && cardNumber.trim().length() > 0)
			query.eq("cardNumber", cardNumber);
		if (type != null && type.getText().trim().length() > 0)
			query.eq("type", type);
		if (trustAccountId != null && trustAccountId.trim().length() > 0)
				query.eq("trustAccountId", trustAccountId);
		if (order != null)
		{
			if (order.name().equalsIgnoreCase("asc"))
			{
				query.asc(orderField);
			}
			else
			{
				query.desc(orderField);
			}
		}
		query.setIsSearchTotalCount(true);
		query.setPageIndex(indexPage);
		query.setPageSize(pageSize);
		QueryResult<UserInfo> requery = serviceFactory.getPersistService().findWithPage(UserInfo.class, query);
		ServiceResult<List<UserInfo>> result = new ServiceResult<List<UserInfo>>(requery.getResults());
		result.setReturnListSize((int) requery.getTotalCount());
		return result;
	}
	@Override
	public List<UserInfo> getGuartors()
	{
		DynamicQuery query = new DynamicQuery(UserInfo.class);
		query.eq("type", UserInfoType.GUARATOR);
		return serviceFactory.getPersistService().find(UserInfo.class, query);
	}

	@Override
	public UserInfo getById(Long id)
	{
		return serviceFactory.getPersistService().get(UserInfo.class, id);
	}

	@Override
	public UserInfo getByUserName(String userName)
	{
		DynamicQuery query = new DynamicQuery(UserInfo.class);
		query.eq("userName", userName);
		List<UserInfo> list = serviceFactory.getPersistService().find(UserInfo.class, query);
		if (list != null&&list.size()>0)
		{
			return list.get(0);
		}
		else
		{
			return null;
		}
	}
}
